#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRSIZE 45


void clear_str(char *c, int n);

int main(int argc, char *argv[]) {

    FILE *fp;
    char aux[STRSIZE];
    char **text = (char**)malloc(sizeof(char*));
    int i = 0;
    int j = 0;
    int n = 0;
    int pa = 0;
    int chunk = 0;

    if ( (fp = fopen(argv[1],"r")) == NULL ) {
        printf("Erro ao abrir o arquivo.\n");
        exit(1);
    }

    clear_str(aux,STRSIZE);

    while(!feof(fp)) {

        fscanf(fp,"%s",aux);
        //printf("%s\n",aux);
        text = (char**)realloc(text,sizeof(char*)*(n+1));
        text[i] = (char*)malloc(sizeof(char)*(strlen(aux)+1));
        strcpy(text[i],aux);
        clear_str(aux,STRSIZE);
        n++;
        i++;
    }


     for ( i = 0; i < n-1; i++ )
            pa += palindromo(text[i]);

    printf("\n%d\n",pa);

    free(*text);
    free(text);
    fclose(fp);

    fflush(stdout);


    return 0;

}

void clear_str(char *c, int n) {

    int i = 0;

    for ( i = 0; i < n; i++ ) {
        c[i] = 0;
    }

}

int palindromo(char *str) {

    int i,l,j;
    int ssize = strlen(str);

    i=0;
    j=ssize-1;
    //ignorando caracteres especiais no final de palavra, no final da palavra, exem.: (kfbrwiufh)
    for(l=j;l>=0;l--)
	if((int)str[l]<65 || ((int)str[l]>90 && (int)str[l]<97) || (int)str[l]>122 )
		j--;
	else
		break;

    //ignorando caracteres especiais no inicio de palavra, no final da palavra, exem.: (kfbrwiufh)
    for(l=0;l<=j;l++)
	if((int)str[l]<65 || ((int)str[l]>90 && (int)str[l]<97) || (int)str[l]>122 )
		i++;
	else
		break;
    
    //se passar aq é palimdrome
    if(j==-1)
	return 0;//isso significa e existe uma palavra com apenas simbolos especiais
    for (  ;i < j;i++)
    {
        if (   ((int)str[j] != (int)str[i])   &&         ((int)str[j]+32 != (int)str[i])   && ((int)str[j] != (int)str[i]+32)         )
		return 0;
        j--;
    }
    //printf("%s ",str);
    return 1;

}

